package com.atguigu.gmall.product.controller;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author mqx
 * @date 2020-12-8 10:53:00
 */
//  测试异步编排
public class CompletableFutureDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //  A ---- "hello"
        //  B ---- "hello word" 1   3
        //  C ---- "hello boy"  3   1
        CompletableFuture<String> FutureA = CompletableFuture.supplyAsync(new Supplier<String>() {
            @Override
            public String get() {
                return "hello";
            }
        });

        //  FutureB
        CompletableFuture<Void> FutureB = FutureA.thenAcceptAsync(new Consumer<String>() {
            @Override
            public void accept(String s) {
                delaySec(3);
                printCurrTime(s+"\t word");
            }
        });
        //  FutureC
        CompletableFuture<Void> FutureC = FutureA.thenAcceptAsync(new Consumer<String>() {
            @Override
            public void accept(String s) {
                delaySec(1);
                printCurrTime(s + "\t boy");
            }
        });

        //  调用方法
        System.out.println(FutureB.get());
        System.out.println(FutureC.get());


        //  没有返回值
        //  CompletableFuture.runAsync(()->{});
        //        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(new Runnable() {
        //            @Override
        //            public void run() {
        //                System.out.println("你来了。");
        //            }
        //        });
        //        completableFuture.get();
        //  有返回值
        //        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(new Supplier<Integer>() {
        //            @Override
        //            public Integer get() {
        //                System.out.println("来人了，开始接客了.");
        //                //  int i = 1/0;
        //                return 1024;
        //            }
        //        }).thenApply(new Function<Integer, Integer>() {
        //            //  获取上一个方法的返回值，同时进行自己的业务处理。并返回自己的结果
        //            @Override
        //            public Integer apply(Integer integer) {
        //                System.out.println("thenApply:\t"+integer);
        //                return integer*2;
        //            }
        //        }).whenComplete(new BiConsumer<Integer, Throwable>() {
        //            @Override
        //            public void accept(Integer integer, Throwable throwable) {
        //                System.out.println("integer:"+integer);
        //                System.out.println("throwable:"+throwable);
        //            }
        //        }).exceptionally(new Function<Throwable, Integer>() {
        //            @Override
        //            public Integer apply(Throwable throwable) {
        //                System.out.println("exceptionally----throwable:"+throwable);
        //                //  异常处理;
        //                return 404;
        //            }
        //        });
        //
        //        System.out.println(integerCompletableFuture.get());
    }

    private static void printCurrTime(String s) {
        System.out.println(s);
    }

    private static void delaySec(int i) {
        try {
            Thread.sleep(i*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
